package com.wandian.texas.command.texas;

import com.wandian.centerserver.CenterMgr;
import com.wandian.centerserver.CenterSessionMgr;
import com.wandian.command.Cmd;
import com.wandian.command.UserCommand;
import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.GameData;
import com.wandian.texas.UserMgr;
import com.wandian.texas.db.DBMgr;
import com.wandian.texas.db.DaoManager;

/**
 * @author long
 * @date 2012-12-8 下午4:08:54
 * @qq 108232706
 * @description GameServerLoginCmd游戏服务器登录验证
 */
@Cmd(code = CenterSessionMgr.CMD_CENTER_LOGIN, desc = "")
public class GameServerLoginCmd extends UserCommand {
	public GameServerLoginCmd() {
		this.setCommandName("GameServerLoginCmd");
	}

	@Override
	public void execute(BaseCommandObject cmdObject) {
		super.execute(cmdObject);
		int serverid = cmdObject.readInt();
		// 应该判断该id是否登陆，更新数据状态，防止一个serverid多处登陆
		// String ServerIp;
		/*Result rs = new Result() {
			@Override
			public void run() {
				try {
					if (!this.IsSuccess || this.RowSize < 1)
						return;
					this.DataRows = new Object[1][2];
					if (this.ResultSet.next()) {
						this.DataRows[0][0] = this.ResultSet.getString(1);
						this.DataRows[0][1] = this.ResultSet.getString(1);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		};
		CommonDaoManager.GameRoomDao(0).getSelect(rs, "t_ServerList", "where State=0 And ServerId=" + serverid, " ServerIp,ServerKey", "");
		*/
		boolean checkSuc = true;
		if (DaoManager.GameRoomDao(0).executeProcedureReturn("sp_ServerLogin", DBMgr.TexasPokerDB, serverid, cmdObject.sessionObject.ipAddress) != 1) {
			// 程序update失败
			checkSuc = false;
			cmdObject.setStatus(GameData.WAITINGCHECKSUCC);
			cmdObject.setCmdStatus(1, (byte) 0);// 继续check login
		}else{
			cmdObject.setStatus(GameData.CHECKUSERSUCC);
			UserMgr.I().loginOn(serverid, cmdObject.sessionObject);
			// 设置当前session的服务器id
			cmdObject.sessionObject.setAttribute("serverid", serverid);
		}	
		cmdObject.writeBegin();
		cmdObject.writeByte(checkSuc ? (byte) 0 : (byte) 1);
		cmdObject.writeEnd();
		UserMgr.I().SendData(cmdObject);
		// 验证合法的游戏端发送积宝
		if (checkSuc) {
			CenterMgr.sendUpdateCornucopiaPer30s(cmdObject);
			//更新状态为登陆
		}
	}
}
